如何使用alert()查看JavaScript中的数组结构? 您所在的位置:网站首页 alert用法 js 如何使用alert()查看JavaScript中的数组结构?

如何使用alert()查看JavaScript中的数组结构?

2023-03-26 01:32| 来源: 网络整理| 查看: 265

如果您希望使用alert()来显示对象数组的内容,我建议您在对象中定义方法toString(),这样就可以使用一个简单的alert(MyArray);数组的完整内容将显示在警报中。

下面是一个示例:

//------------------------------------------------------------------- // Defininf the Point object function Point(CoordenadaX, CoordenadaY) { // Sets the point coordinates depending on the parameters defined switch (arguments.length) { case 0: this.x = null; this.y = null; break; case 1: this.x = CoordenadaX; this.y = null; break; case 2: this.x = CoordenadaX; this.y = CoordenadaY; break; } // This adds the toString Method to the point object so the // point can be printed using alert(); this.toString = function() { return " (" + this.x + "," + this.y + ") "; }; }

然后,如果你有一个点数组:

var MyArray = []; MyArray.push ( new Point(5,6) ); MyArray.push ( new Point(7,9) );

您只需调用以下命令即可打印:

alert(MyArray);

希望这能有所帮助!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有